package ru.mcfr.oxygen.framework.operations.table;

import ro.sync.ecss.extensions.api.ArgumentDescriptor;
import ro.sync.ecss.extensions.api.AuthorOperationException;
import ro.sync.ecss.extensions.api.node.AuthorElement;
import ro.sync.ecss.extensions.api.node.AuthorNode;
import ru.mcfr.oxygen.framework.operations.McfrBaseAuthorOperation;

import javax.swing.text.BadLocationException;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: ws
 * Date: 23.06.11
 * Time: 16:05
 * To change this template use File | Settings | File Templates.
 */
public class CopyColumnWidthOperation extends McfrBaseAuthorOperation {

    {
        description = "Копирование ширины и колспана колонок в другие колонки";

        argumentsNameArray = new String[]{"arg"};

        argumentsTypesArray = new Object[]{ArgumentDescriptor.TYPE_STRING};

        argumentsDescriptionsArray = new String[]{"arg"};
    }

    private Vector<String> colspans = new Vector<String>();

    @Override
    public void doMainOperation() {
        AuthorNode thisElement = selectedElement;
        while (!thisElement.getName().equalsIgnoreCase("строка"))
            thisElement = thisElement.getParent();
        int i = 0;
        for (AuthorNode cell : ((AuthorElement) thisElement).getContentNodes()){
            authorAccess.getWorkspaceAccess().showErrorMessage(thisElement.getName() + " => " + cell.getName());//
            for (AuthorNode node : ((AuthorElement) cell).getContentNodes()){
                if (node.getType() == AuthorNode.NODE_TYPE_ELEMENT){
                    String text = "some text " + (++i);
                    int offset = node.getStartOffset() + node.getDisplayName().length() + 2 + text.length() * (i - 1);
//                    try {
//                        AuthorDocumentFragment frag = controller.createNewDocumentTextFragment("some text " + (++i));
//                        controller.insertFragment(offset, frag);
//                    } catch (AuthorOperationException e) {
//                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                    }
                    authorAccess.getWorkspaceAccess().showErrorMessage(thisElement.getDisplayName() + " => " + cell.getDisplayName() + " => " + node.getDisplayName());
                    controller.insertText(offset, text);
                    break;
                }
            }
        }
        /*
        authorAccess.showErrorMessage(thisElement.getName());
        if (thisElement != null){
            if (argumentsValues.get(argumentsNameArray[0]).equalsIgnoreCase("copy")){ // copy parameters to bufer
                try{
                AuthorElement[] cells = ((AuthorElement) thisElement).getElementsByLocalName("ячейка");
                for (AuthorElement cell : cells)
                    colspans.add(cell.getAttribute("colspan").getValue());
                } catch (Exception e){
                    authorAccess.showErrorMessage(e.getMessage());
                }
            } else if (argumentsValues.get(argumentsNameArray[1]).equalsIgnoreCase("apply")){ // apply parameters from bufer
                AuthorElement[] cells = ((AuthorElement) thisElement).getElementsByLocalName("ячейка");
                if (colspans.size() != 0){
                    int diff = colspans.size() - cells.length;
                    authorAccess.showErrorMessage("diff:" + diff);
                    if (diff > 0){
                        AuthorNode lastCell = cells[cells.length];
                        try {
                            AuthorDocumentFragment fragment = controller.createNewDocumentFragmentInContext("<ячейка/>", lastCell.getEndOffset());
                            for (int i = 0; i < diff; i++){
                                controller.insertFragment(lastCell.getEndOffset(), fragment);
                                authorAccess.showErrorMessage("insert new fragment");
                            }
                        } catch (AuthorOperationException e) {
                            e.printStackTrace();
                        }
                    }
                    // if deiff < 0 not see here right now

                    cells = ((AuthorElement) thisElement).getElementsByLocalName("ячейка");
                    authorAccess.showErrorMessage("cells count: " + cells.length);
                    int o = 0;
                    for (String colspan : colspans){
                        if (!colspan.isEmpty()){
                            controller.setAttribute("colspan", new AttrValue(colspan), (AuthorElement) cells[o]);
                            authorAccess.showErrorMessage("set collspan for cell: " + o);
                        }
                            //((AuthorElement) cells.get(o)).setAttribute("colspan", new AttrValue(colspan));
                        o++;
                    }

                }
            }
        } */
    }

    @Override
    public void doOperationWithSelected() throws AuthorOperationException, BadLocationException {

    }

    @Override
    public void doOperationAtCaretPos() throws AuthorOperationException {

    }
}
